package com.xm.controller.web;

import com.xm.pojo.FlowHistory;
import com.xm.service.FlowHistoryService;
import com.xm.util.JSONResult;
import com.xm.util.excel.ExcelUtils;
import com.xm.util.page.PageUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by wzl on 2018/4/16.
 */
@Controller
@RequestMapping("/web/history")
public class FlowHistoryController {
    @Resource
    private FlowHistoryService flowHistoryService;

    /**
     * 分页查询某设备历史流量数据方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectFlowHistoryPage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowHistoryPage(HttpServletRequest request, FlowHistory flowHistory) {
        JSONResult jsonResult = new JSONResult();
        flowHistory.setStart(Integer.parseInt(request.getParameter("start")));
        flowHistory.setLength(Integer.parseInt(request.getParameter("length")));
        //分页查询某设备流量数据
        List<FlowHistory> flowHistoryReportList = flowHistoryService.selectFlowHistoryPage(flowHistory);
        //封装返回分页数据
        int total = flowHistoryService.queryFlowHistoryTotal(flowHistory);
        int length = flowHistory.getLength();
        int start = flowHistory.getStart();
        PageUtils pager = new PageUtils(flowHistoryReportList, total, length, start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 某个设备历史流量数据导出
     */
    @RequestMapping(value = "/excelHistoryDownload")
    public void excelHistoryDownload(HttpServletRequest request, HttpServletResponse response, FlowHistory flowHistory) {
        response.reset();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(new Date());
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + dateStr + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //导出逻辑
        List<FlowHistory> creditInfoList = new ArrayList<>();
        creditInfoList = flowHistoryService.selectFlowHistoryExcel(flowHistory);
        List<FlowHistory> ems = new ArrayList<>();
        Map<Integer, List<FlowHistory>> maps = new LinkedHashMap<>();
        SXSSFWorkbook book = null;
        ems.add(new FlowHistory("设备名称", "deviceName", 0));
        ems.add(new FlowHistory("流量", "flow", 0));
        ems.add(new FlowHistory("流速", "speed", 0));
        ems.add(new FlowHistory("正向总量", "totalPositive", 0));
        ems.add(new FlowHistory("反向总量", "totalNegative", 0));
        ems.add(new FlowHistory("报警", "alarm", 0));
        ems.add(new FlowHistory("时间", "excelTime", 0));
        ems.add(new FlowHistory("备注", "remark", 0));
        maps.put(0, ems);
        try {
            book = ExcelUtils.createFlowHistoryExcelFile(FlowHistory.class, creditInfoList, maps, "历史流量数据表");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        OutputStream output;
        try {
            output = response.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            //刷新此缓冲的输出流，保证数据全部都能写出
            bufferedOutPut.flush();
            book.write(bufferedOutPut);
            bufferedOutPut.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
